﻿#include "MaterialDampingWidget.h"
#include "ui_MaterialDampingWidget.h"

#include "FITK_Interface/FITKInterfaceStructural/FITKAbaMaterialDamping.h"

#include <QMessageBox>

namespace GUI
{
    MaterialDampingWidget::MaterialDampingWidget(EventOper::ParaWidgetInterfaceOperator * oper,Interface::FITKAbaMaterialDamping * item, QWidget * parent) :
        MaterialWidgetBase(oper,parent), _materItem(item)
    {
        _ui = new Ui::MaterialDampingWidget;
        _ui->setupUi(this);
        showValue();
    }

    MaterialDampingWidget::~MaterialDampingWidget()
    {
        if (_ui != nullptr)delete _ui;
    }

    void MaterialDampingWidget::showValue()
    {
        if (_materItem == nullptr)return;
        double alpha = _materItem->getAlpha();
        _ui->lineEdit_Alpha->setText(QString::number(alpha));
        double beta = _materItem->getBeta();
        _ui->lineEdit_Beta->setText(QString::number(beta));
        double composite = _materItem->getComposite();
        _ui->lineEdit_Composite->setText(QString::number(composite));
        double structural = _materItem->getStructural();
        _ui->lineEdit_Structural->setText(QString::number(structural));
    }

    bool MaterialDampingWidget::checkValue()
    {
        auto checkValue = [=](QString str, QString type)->bool {
            //判断数据是否有效
            bool isOk = false;
            str.toDouble(&isOk);
            if (isOk == true)return true;

            QString message = QString(tr("Tabular data for at least one option or suboption\n has either blank or zero valued row(s).\n"));
            message = message + QString(tr("Error: Damping %1\n").arg(type));
            QMessageBox::warning(this, tr("StructualApp"), message);
            return false;
        };

        QString value = _ui->lineEdit_Alpha->text();
        if(!checkValue(value,tr("Alpha")))return false;
        value = _ui->lineEdit_Beta->text();
        if(!checkValue(value,tr("Beta")))return false;
        value = _ui->lineEdit_Composite->text();
        if(!checkValue(value,tr("Composite")))return false;
        value = _ui->lineEdit_Structural->text();
        if(!checkValue(value,tr("Structural")))return false;

        return true;
    }

    bool MaterialDampingWidget::saveValue()
    {
        if (_materItem == nullptr)return false;

        double alpha = _ui->lineEdit_Alpha->text().toDouble();
        _materItem->setAlpha(alpha);
        double beta = _ui->lineEdit_Beta->text().toDouble();
        _materItem->setBeta(beta);
        double composite = _ui->lineEdit_Composite->text().toDouble();
        _materItem->setComposite(composite);
        double structural = _ui->lineEdit_Structural->text().toDouble();
        _materItem->setStructural(structural);
        return true;
    }
}
